package com.atguigu.gmall.test;

import sun.nio.ch.ThreadPool;

import java.util.concurrent.ArrayBlockingQueue;
import java.util.concurrent.CompletableFuture;
import java.util.concurrent.ThreadPoolExecutor;
import java.util.concurrent.TimeUnit;

/**
 * @Description:
 * @author: JC
 * @date: 2021/10/28 16:47
 */

public class CompletableFutureDemo {

    public static void main(String[] args) throws Exception{

        // 创建线程池
        ThreadPoolExecutor threadPoolExecutor = new ThreadPoolExecutor(
                2,
                5,
                3,
                TimeUnit.SECONDS,
                new ArrayBlockingQueue<>(3));
        CompletableFuture<String> futureA = CompletableFuture.supplyAsync(() -> "hello");

        CompletableFuture<Void> futureB = futureA.thenAcceptAsync(t -> {
            delaySec(1);
            printCurrTime(t+":futureB");
        });

        futureB.get();
        CompletableFuture<Void> futureC = futureA.thenAcceptAsync(t -> {
            delaySec(3);
            printCurrTime(t + ":futureC");
        });
        futureC.get();
    }

    private static void printCurrTime(String s) {
        System.out.println(s);
    }

    private static void delaySec(int i) {
        try {
            TimeUnit.SECONDS.sleep(1);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }

    }
}
